package logic

import (
	"context"
	errors2 "errors"
	"house/house"
	"house/internal/svc"
	"house/model/mysql"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseUpdateLogic {
	return &HouseUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseUpdateLogic) HouseUpdate(in *house.HouseUpdateRequest) (*house.HouseUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var s mysql.Shop
	s.ID = uint(in.Id)
	if err = s.Find(); err != nil {
		logx.Error("查询失败")
		return nil, errors2.New("查询失败")

	}
	if s.Status == 1 {
		logx.Error("未查询到已上架的房源")
		return nil, errors2.New("未查询到已上架的房源")
	}
	s.Status = int(in.Status)
	mysql.DB.Where("id = ?", s.ID).Updates(&s)
	return &house.HouseUpdateResponse{
		Id: int64(s.ID),
	}, nil
}
